//
//  MyViewController.m
//  Codingke2016
//
//  Created by xujiangtao on 16/5/30.
//  Copyright © 2016年 xujiangtao. All rights reserved.
//

#import "MyViewController.h"
#import "UIScrollView+TwitterCover.h"
#import "UIView+i7Rotate360.h"

@interface MyViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UIScrollView *_mainScrollView;
    UITableView *_mainTableView;
    UIImageView*_headImageView;

}
@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor =[UIColor colorWithHexString:@"#e8edf1"];
    
    [self makeMainUI];
    [self makeTableView];
    [self makeUserHeadView];
    
}
-(void)viewWillAppear:(BOOL)animated{
   
    [super viewWillAppear:animated];
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(startRotate) userInfo:nil repeats:NO];
}
-(void)makeMainUI{

    _mainScrollView=[[UIScrollView alloc] initWithFrame:self.view.bounds];
    [_mainScrollView setContentSize:CGSizeMake(self.view.bounds.size.width,SCREEN_HEIGHT+10)];// 600 要大于568
    NSLog(@"SCREEN_HEIGHT===%lf",SCREEN_HEIGHT);
    if(SCREEN_HEIGHT==480.0){
        [_mainScrollView setContentSize:CGSizeMake(self.view.bounds.size.width,SCREEN_HEIGHT+90)];// 600 要大于568
    }
    _mainScrollView.showsVerticalScrollIndicator=NO;
    _mainScrollView.showsHorizontalScrollIndicator=NO;
    UIImage *image=[UIImage imageNamed:@"img_backStarImage"];
    
    [_mainScrollView addTwitterCoverWithImage:image];
    [self.view addSubview:_mainScrollView];

}
-(void)makeUserHeadView{
  
    UIImage* headImage = [UIImage imageNamed:@"icon_head_my"];
    UIView *coverView=[[UIView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 77)/2,114/2, 77, 77)];
    coverView.backgroundColor=[UIColor clearColor];
    [_mainScrollView addSubview:coverView];
    
    _headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 77,77)];
    [_headImageView setUserInteractionEnabled:YES];
    [[_headImageView layer] setCornerRadius:77/2];
    [[_headImageView layer] setMasksToBounds:YES];
    _headImageView.image =headImage;
    [_headImageView setContentMode:UIViewContentModeScaleAspectFit];
    [coverView addSubview:_headImageView];
    
    //添加tap事件
    UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickLoginButton)];
    [_headImageView addGestureRecognizer:tapGesture];
    
}
// 图片翻转
-(void)startRotate
{
    [_headImageView rotate360WithDuration:2.0 repeatCount:1 timingMode:i7Rotate360TimingModeLinear];
    _headImageView.animationDuration = 2.0;
    
    _headImageView.animationRepeatCount = 1;
    [_headImageView startAnimating];
}

-(void)makeTableView{

    _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 437/2-17, SCREEN_WIDTH, SCREEN_HEIGHT - 100) style:UITableViewStylePlain];
    _mainTableView.backgroundColor = [UIColor clearColor];
    //  [mainTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_mainTableView setDataSource:self];
    [_mainTableView setDelegate:self];
    [_mainScrollView addSubview:_mainTableView];
    
    UIView* footerView = [[UIView alloc] init];
    footerView.backgroundColor = [UIColor whiteColor];
    _mainTableView.tableFooterView = footerView;

}
#pragma mark - TableView Delegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if(section ==0){
        return 2;
    }else if (section==1){
        return 2;
    }else{
        return 1;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 20;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
   
    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,20)];
    sectionView.backgroundColor =[UIColor clearColor];
    
    return sectionView;

}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"ID"];
    if(cell==nil){
    
        cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"];
        //图标
        UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:@"icon_my_%d_%d",(int)indexPath.section,(int)indexPath.row]];
        UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, (50 - image.size.height) / 2, image.size.width, image.size.height)];
        [imageView setImage:image];
        [cell addSubview:imageView];
        
        
        //文字描述
        UILabel* label = [[UILabel alloc] initWithFrame:CGRectZero];
        [label setBackgroundColor:[UIColor clearColor]];
        [label setTextColor:[UIColor blackColor]];
        //[label setFont:[UIFont fontWithName:KAKU_FONT size:17]];
        label.font =[UIFont systemFontOfSize:18];
        [cell addSubview:label];
        
        
        NSString* labelStr = @"";
        if (indexPath.section == 0) {
            if (indexPath.row == 0) {
                labelStr = @"我的课程";
            }else if (indexPath.row == 1){
                labelStr = @"我的收藏";
            }
        }else if (indexPath.section == 1){
            if (indexPath.row == 0) {
                labelStr = @"我的下载";
            }else if (indexPath.row == 1){
                labelStr = @"我的帖子";
            }
        }else{
             labelStr = @"设置";
        }
        label.text = labelStr;
        [label sizeToFit];
        
        CGRect labelFrame = label.frame;
        labelFrame.origin.x = 50;
        labelFrame.origin.y = (50 - label.frame.size.height) / 2;
        label.frame = labelFrame;
        
        cell.backgroundColor = [UIColor whiteColor];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.clipsToBounds = YES;

    }
    
    return cell;
}
-(void)clickLoginButton{
  
    NSLog(@"点击头像");

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
